#!/bin/sh

# logout.sh - runs all hooks in /private/etc/hooks/logout

LOGOUT_HOOK_DIR="/private/etc/hooks/logout"

errorState=0

logger -p user.info "Beginning logout hooks for user ${1}..."

if [ ! -d "${LOGOUT_HOOK_DIR}" ]; then
	# Logout hooks dir doesn't exist. Exit.
	logger -s -p user.info "No logout hooks directory. Exiting."
	exit 1
else
	for hook in "${LOGOUT_HOOK_DIR}"/*; do
		if [ -s "${hook}" -a -x "${hook}" ]; then
			logger -p user.info "Starting hook ${hook}."
			${hook} $*
			rc=$?
			if [ $rc -ne 0 ]; then
				logger -s -p user.info \
					"Error: ${hook} exited with status ${rc}!" 1>&2
				errorState=1
			else
				logger -p user.info "Hook ${hook} finished."
			fi
		fi
	done
fi

if [ $errorState -ne 0 ]; then
	logger -s -p user.info "Logout hooks complete, but with errors."
else
	logger -p user.info "Logout hooks complete."
fi
